//
//  SYueduyijiViewController.m
//  News
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SYueduyijiViewController.h"
#import "SyijitableViewTableViewCell.h"
#import "SYueduViewController.h"
#import "SNetWorkHandle.h"
#import "SReadyijiModel.h"
#import "SyijiWenziTableViewCell.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"

@interface SYueduyijiViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIImageView *toupian;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;

@end

@implementation SYueduyijiViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.array = [NSMutableArray array];
    //请求出现小菊花
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.tag = 1000;
    [view setBackgroundColor:[UIColor blackColor]];
    view.alpha = 0.5;
    [self.view addSubview:view];
    [view release];
    self.shu = 20;
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:view.center];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [view addSubview:_activityIndicator];
    
    [_activityIndicator startAnimating];
    
    
   // self.title = @"一级详情";
    [self jianlitableView];
    //先默认下拉
    self.isUpLoading = NO;
    //集成上啦下拉功能
    [self addHeader];
    [self addFooter];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style: UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
    self.title = self.biaoti;
}

- (void)fanhui
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 下拉刷新
- (void)addHeader
{
    __block SYueduyijiViewController *vc = self;
    
    //添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
        
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getData];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
}

#pragma mark - 上啦加载更多
- (void)addFooter
{
    __block SYueduyijiViewController *vc = self;
    //添加上啦刷新尾部控件
    [self.tableView addFooterWithCallback:^{
        //进入刷新状态就会回调这个block
        vc.isUpLoading = YES; //标记为上啦操作
        [vc getData1];
        
        self.shu = _shu + 20;
    }];
}


- (void)getData
{
    
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/0-20.html", self.str];
    //NSLog(@"%@", _str);
    
    [SNetWorkHandle getDataWithURL:str completion:^(id result) {
       NSMutableDictionary *dic = result;
        NSMutableArray *arr = [dic objectForKey:self.str];
       
        
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.array removeAllObjects];
        }

        
        for (NSMutableDictionary *temp in arr) {
           
            SReadyijiModel *yiji = [[SReadyijiModel alloc] initWithDic:temp];
            
    
            [_array addObject:yiji];
            
            
            
        }
        [self.tableView footerEndRefreshing];//结束底部刷新效果
        [self.tableView headerEndRefreshing];//结束头部刷新效果

        [_tableView reloadData];
    }];
}

//上拉加载获取更多数据
- (void)getData1
{
    if (self.shu < 220) {
        

    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/%ld-20.html", self.str, (long)self.shu];
    //NSLog(@"%ld",_shu);
   // NSLog(@"%@", _str);
    [SNetWorkHandle getDataWithURL:str completion:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableArray *arr = [dic objectForKey:self.str];
        
        for (NSMutableDictionary *temp in arr) {
            
            SReadyijiModel *yiji = [[SReadyijiModel alloc] initWithDic:temp];
            [_array addObject:yiji];
        }
        [self.tableView footerEndRefreshing];//结束底部刷新效果
        //[self.tableView headerEndRefreshing];//结束头部刷新效果
        [_tableView reloadData];
    }];}else{
        
        [self.tableView footerEndRefreshing];
    }
}
/**
 *  建立tableView
 */
- (void)jianlitableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //一般用于添加顶部广告轮播图
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)] autorelease];
    view.backgroundColor = [UIColor redColor];
    _tableView.tableHeaderView = view;
    
    self.toupian = [[UIImageView alloc] initWithFrame:view.bounds];
    _toupian.image = [UIImage imageNamed:@"yinji"];

    [view addSubview:_toupian];
    [self.view addSubview:_tableView];
    [_tableView release];
}
/**
 *  实现协议方法
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SReadyijiModel *yiji = _array[indexPath.row];
    
    SyijitableViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    SyijiWenziTableViewCell *wenzi = [tableView dequeueReusableCellWithIdentifier:@"wenzi"];
    if (yiji.imgsrc.length == 0) {
        
        if (wenzi == nil) {
            wenzi = [[SyijiWenziTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"wenzi"];
        }
        
        wenzi.yiji = yiji;
        return wenzi;
        
    }else{
    
    if (cell == nil) {
        
        cell = [[SyijitableViewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
        cell.yiji = yiji;
        return cell;}
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.view.frame.size.height ) / 3;
}
//点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SYueduViewController *yuedu = [[SYueduViewController alloc] init];
         yuedu.str = [_array[indexPath.row] docid];
//    SReadyijiModel *yiji = _array[indexPath.row];
                           // yuedu.biaoti = yiji.title;
//    NSLog(@"%@", yuedu.title);
  [self.navigationController pushViewController:yuedu animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
